# 使用十折交叉验证法验证逻辑回归模型

import numpy as np
import pandas as pd
from cmath import sqrt
from LogisticRegression import LogisticRegression


lr = LogisticRegression()
# 读取数据集
print("----------\n正在读取数据集...")
data = pd.read_excel("/Users/liuyuanxi/学习/华为智能基座/附件2.课程资源/数据集/员工离职预测模型.xlsx")
print("成功读取数据集，共 %d 条样本\n"%(int(len(data))))
# 将读入的数据集转换为数组
data = np.array(data)
# 将数据集划分为特征向量和结果
print("----------\n正在对样本特征向量及类别进行划分...")
featureVect = []
for i in range(int(len(data))):
    temp = []
    for j in range(int(len(data[0]))-2):
        temp.append(data[i][j])
    featureVect.append(temp)
resultData = []
for i in range(int(len(data))):
    resultData.append(data[i][-1])
print("样本划分完毕")
# 将数据集归一化
print("----------\n正在对数据进行归一化...")
# 归一化特征向量
for i in range(int(len(featureVect[0]))):
    biggestNum = 0 # 初始化最大数值
    for j in range(int(len(featureVect))):
        if featureVect[j][i] > biggestNum:
            biggestNum = featureVect[j][i]
        else:
            continue
    for m in range(int(len(featureVect[0]))):
        featureVect[m][i] = featureVect[m][i]/biggestNum
print("数据归一化完成")
# 将数据集划分为十份
print("----------\n正在根据十折交叉验证法划分数据...")
x1, x2, x3, x4, x5, x6, x7, x8, x9, x10 = featureVect[0:1500], featureVect[1500:3000], featureVect[3000:4500], featureVect[4500:6000], featureVect[6000:7500], featureVect[7500:9000], featureVect[9000:10500], featureVect[10500:12000], featureVect[12000:13500], featureVect[13500:15000]
y1, y2, y3, y4, y5, y6, y7, y8, y9, y10 = resultData[0:1500], resultData[1500:3000], resultData[3000:4500], resultData[4500:6000], resultData[6000:7500], resultData[7500:9000], resultData[9000:10500], resultData[10500:12000], resultData[12000:13500], resultData[13500:15000]
xi = x1+x2+x3+x4+x5+x6+x7+x8+x9+x10
yi = y1+y2+y3+y4+y5+y6+y7+y8+y9+y10
print("数据划分完成")
# 使用十折交叉验证法验证模型
print("----------\n正在训练模型...")
losslist_train = []
losslist_pred = []
MSE = []
for i in range(10):
    # 第 i 份数据作为验证集，其他作为训练集
    x_train = []
    y_train = []
    x_train.extend(xi[:i*1500]+xi[(i+1)*1500:])
    y_train.extend(yi[:i*1500]+yi[(i+1)*1500:])
    x_pred = xi[i*1500:(i+1)*1500]
    y_pred = yi[i*1500:(i+1)*1500]
    # 进行模型的训练
    GD_Fit_Losslist_Train = lr.GD_Fit(x_train, y_train, 5e-2, 30)
    losslist_train.extend(GD_Fit_Losslist_Train)
    # 进行模型的验证
    Pred_Label = []
    Pred_Label = lr.predict(x_pred, 0.63)
    # 计算模型预测的误差
    MSE_sum = 0.0
    for i in range(int(len(Pred_Label))):
        MSE_sum += sqrt((Pred_Label[i]-y_pred[i])**2)
    MSE.append(MSE_sum/len(Pred_Label))
print("模型训练完毕\n")
print("MSE = ", MSE)
# 画出损失值函数
lr.training_visualize(losslist_train)
